package com.xuxin.nantai.activity.quan;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xuxin.nantai.R;
import com.xuxin.nantai.base.BaseActivity;
import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.bean.BaseBean;
import com.xuxin.nantai.bean.DefaultGroupBean;
import com.xuxin.nantai.bean.FlowLineBean;
import com.xuxin.nantai.bean.GroupBean;
import com.xuxin.nantai.bean.ProducerBean;
import com.xuxin.nantai.contract.CompleteDetailUpdateContract;
import com.xuxin.nantai.presenter.CompleteDetailUpdatePresenter;
import com.xuxin.nantai.utils.ACache;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.06.09 0009
 * 缝制全检明细新增修改
 */
public class CompleteDetailUpdateActivity extends BaseActivity implements CompleteDetailUpdateContract.View {
    private CompleteDetailUpdateContract.Presenter presenter = new CompleteDetailUpdatePresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;

    @BindView(R.id.complete_detail_update_group)
    TextView complete_detail_update_group;//生产组别
    @BindView(R.id.complete_detail_update_producer)
    TextView complete_detail_update_producer;//生产人员
    @BindView(R.id.complete_detail_update_desc)
    TextView complete_detail_update_desc;//瑕疵描述
    @BindView(R.id.complete_detail_update_defect)
    TextView complete_detail_update_defect;//疵点数

    private String Guid;//唯一Guid
    private String DID;//唯一DID
    private String HID;//唯一DID
    private String GroupName;//生产组别名称
    private String GroupCode;//生产组别代码
    private String ProductName;//生产人员名字
    private String ProductCode;//生产人员代码
    private String DefectDes;//疵点描述
    private String DefectNum;//疵点数量

    private MaterialDialog materialDialog;
    private String flowLineName;//流水线

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_complete_detail_update);
    }

    @Override
    public void initView() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            HID = bundle.getString("HID");//新增修改都需要
            if (!StringUtils.isEmpty(bundle.getString("Guid"))) {
                title_name.setText("修改明细");
                Guid = bundle.getString("Guid");
                DID = bundle.getString("DID");
                GroupName = bundle.getString("GroupName");
                GroupCode = bundle.getString("GroupCode");
                ProductName = bundle.getString("ProductName");
                ProductCode = bundle.getString("ProductCode");
                DefectDes = bundle.getString("DefectDes");
                DefectNum = bundle.getString("DefectNum");
                complete_detail_update_group.setText(GroupName);
                complete_detail_update_producer.setText(ProductName);
                complete_detail_update_desc.setText(DefectDes);
                complete_detail_update_defect.setText(DefectNum);
                LogUtils.e("缝制全检_修改明细_HID = " + HID);
                LogUtils.e("缝制全检_修改明细_Guid = " + Guid);
            } else {
                title_name.setText("新增明细");
                LogUtils.e("缝制全检_新增明细_HID = " + HID);
                LogUtils.e("缝制全检_新增明细_Guid = " + Guid);
            }
        }
    }

    @Override
    public void initData() {
        handler.sendEmptyMessage(0);
        //修改明细通过GroupCode获取生产人员
        handler.sendEmptyMessage(1);
        if (StringUtils.isEmpty(Guid)) {
            //新增明细需要给个默认的生产组别
            handler.sendEmptyMessage(3);
        }
    }

    @Override
    public void onComplete() {
        if (null != materialDialog) {
            materialDialog.dismiss();
        }
    }

    @Override
    @OnClick({R.id.title_left, R.id.complete_detail_update_group, R.id.complete_detail_update_producer,
            R.id.complete_detail_update_gain, R.id.complete_detail_update_update})
    public void onClick(View view) {
        KeyboardUtils.hideSoftInput(this);
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.complete_detail_update_group:
                if (null != groupBean && null != groupBean.getData() && groupBean.getData().size() > 0) {
                    new MaterialDialog.Builder(this)
                            .title("请选择生产组别")
                            .items(groupBean.getData())
                            .itemsCallback((dialog, view1, position, text) -> {
                                try {
                                    GroupName = groupBean.getData().get(position).getCDepName();
                                    GroupCode = groupBean.getData().get(position).getCDepCode();
                                    complete_detail_update_group.setText(GroupName);
                                    ProductName = null;
                                    ProductCode = null;
                                    complete_detail_update_producer.setText("");
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            })
                            .show();
                } else {
                    ToastUtils.showShort("暂无生产组别数据");
                }
                break;
            case R.id.complete_detail_update_producer:
                if (null != flowLineBean && null != flowLineBean.getData() && flowLineBean.getData().size() > 0) {
                    new MaterialDialog.Builder(this)
                            .title("请先选择流水线")
                            .items(flowLineBean.getData())
                            .itemsCallback((dialog, view1, position, text) -> {
                                flowLineName = flowLineBean.getData().get(position).getLineName();
                                handler.sendEmptyMessage(4);
                            })
                            .show();
                }
                break;
            case R.id.complete_detail_update_gain:
                try {
                    Bundle bundle_2 = new Bundle();
                    bundle_2.putString("Guid", Guid);
                    bundle_2.putString("InsType", "缝制全检");
                    ActivityUtils.startActivityForResult(bundle_2, this,
                            DefectActivity.class, BaseConstant.result_code);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.complete_detail_update_update:
                DefectDes = complete_detail_update_desc.getText().toString();
                DefectNum = complete_detail_update_defect.getText().toString();
                if (StringUtils.isEmpty(GroupName)) {
                    ToastUtils.showShort("请选择生产组别");
                    return;
                }
                if (StringUtils.isEmpty(ProductName)) {
                    ToastUtils.showShort("请选择生产人员");
                    return;
                }
                if (StringUtils.isEmpty(DefectNum)) {
                    ToastUtils.showShort("疵点数错误");
                    return;
                }
                materialDialog = new MaterialDialog.Builder(this)
                        .content("提交数据中...")
                        .progress(true, 0)
                        .cancelable(false)
                        .show();
                handler.sendEmptyMessage(2);
                break;
        }
    }

    private Handler handler = new Handler(message -> {
        String LoginName = ACache.get(this).getAsString("LoginName");
        String UserName = ACache.get(this).getAsString("UserName");
        switch (message.what) {
            case 0:
                presenter.getGroups();
                break;
            case 1:
                presenter.getFlowLine();
                break;
            case 2:
                LogUtils.e(" Guid = " + Guid);
                LogUtils.e(" DID = " + DID);
                LogUtils.e(" HID = " + HID);
                LogUtils.e(" GroupName = " + GroupName);
                LogUtils.e(" GroupCode = " + GroupCode);
                LogUtils.e(" ProductName = " + ProductName);
                LogUtils.e(" ProductCode = " + ProductCode);
                LogUtils.e(" DefectDes = " + DefectDes);
                LogUtils.e(" DefectNum = " + DefectNum);
                LogUtils.e(" LoginName = " + LoginName);
                LogUtils.e(" UserName = " + UserName);
                presenter.update(LoginName, UserName,
                        Guid, DID, HID, GroupName, GroupCode, ProductName, ProductCode, DefectDes, DefectNum);
                break;
            case 3:
                presenter.getDefaultGroup(LoginName);
                break;
            case 4:
                if (!StringUtils.isEmpty(flowLineName) && !StringUtils.isEmpty(GroupCode)) {
                    LogUtils.e("flowLineName = " + flowLineName);
                    LogUtils.e("GroupCode = " + GroupCode);
                    presenter.getProducer(GroupCode, flowLineName);
                }
                break;
        }
        return false;
    });

    private GroupBean groupBean;

    @Override
    public void onSuccess(GroupBean groupBean) {
        this.groupBean = groupBean;
    }

    private FlowLineBean flowLineBean;

    @Override
    public void onSuccess(FlowLineBean flowLineBean) {
        this.flowLineBean = flowLineBean;
    }

    @Override
    public void onSuccess(ProducerBean producer) {
        if (null != producer && null != producer.getData() && producer.getData().size() > 0) {
            new MaterialDialog.Builder(this)
                    .title("请选择生产人员")
                    .items(producer.getData())
                    .itemsCallback((dialog, view1, position, text) -> {
                        ProductName = producer.getData().get(position).getStaffName();
                        ProductCode = producer.getData().get(position).getStaffNumber();
                        complete_detail_update_producer.setText(ProductName);
                    })
                    .show();
        } else {
            ToastUtils.showShort("流水线暂无生产人员");
        }
    }

    @Override
    public void onSuccess(DefaultGroupBean defaultGroupBean) {
        if (null != defaultGroupBean) {
            GroupName = defaultGroupBean.getGroupName();
            GroupCode = defaultGroupBean.getGroupCode();
            complete_detail_update_group.setText(GroupName);
        }
    }

    @Override
    public void onSuccess(BaseBean baseBean) {
        ToastUtils.showShort(baseBean.getMessage());
        setResult(BaseConstant.result_code);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (BaseConstant.result_code == resultCode && null != data) {
            DefectDes = data.getStringExtra("DefectDes");
            DefectNum = data.getStringExtra("DefectNum");
            complete_detail_update_desc.setText(DefectDes);
            complete_detail_update_defect.setText(DefectNum);
        }
    }
}